package com.zhf.eduuserboot.controller;

import com.alibaba.fastjson.JSON;
import com.zhf.eduuserboot.commons.HttpClientUtil;
import com.zhf.eduuserboot.entity.Token;
import com.zhf.eduuserboot.entity.User;
import com.zhf.eduuserboot.entity.UserDTO;
import com.zhf.eduuserboot.entity.WxUser;
import com.zhf.eduuserboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2021/2/14 14:25
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class WxLoginController {

    @Autowired
    private UserService userService;

    //判断是否用微信登录成功
    private UserDTO userDTO=null;

    @GetMapping("wxlogin")
    public Object wxlogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        System.out.println("【临时凭证】code:"+code);
        //通过code想微信官方生气一个令牌 token
        //发出一个get请求
        String url= "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd99431bbff8305a0&secret=60f78681d063590a469f1b297feff3c4&code=" + code + "&grant_type=authorization_code";
        String tokenString = HttpClientUtil.doGet(url);
        //将token转换成实体类对象
        Token token = JSON.parseObject(tokenString, Token.class);
        System.out.println("token" + tokenString);

        //通过token去微信官方获取用户信息
        String getUser="https://api.weixin.qq.com/sns/userinfo?access_token=" + token.getAccess_token() + "&openid=" + token.getOpenid();
        String userInfoString = HttpClientUtil.doGet(getUser);
        System.out.println("用户信息"+userInfoString);
        WxUser wxUser = JSON.parseObject(userInfoString, WxUser.class);
        System.out.println("微信昵称 = " + wxUser.getNickname());
        System.out.println("微信头像 = " + wxUser.getHeadimgurl());
//        return "user/login?phone="+wxUser.getUnionid()+"&password="+wxUser.getUnionid()+"&nickName="+wxUser.getNickname()+"&headImg="+wxUser.getHeadimgurl();
        String phone = wxUser.getUnionid();
        String password = wxUser.getUnionid();
        String nickName = wxUser.getNickname();
        String headImg = wxUser.getHeadimgurl();

        //走一遍登录注册流程
        User user=null;
        userDTO = new UserDTO();
        System.out.println("phone: " + phone);
        System.out.println("password: " + password);
        //检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if (i==0){
            userService.register(phone, password,nickName,headImg);
            userDTO.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
            userDTO.setState(200);
        }else{
            user = userService.login(phone, password);
            if (user != null) {
                userDTO.setState(200);
                userDTO.setMessage("登录成功！");
            }else{
                userDTO.setState(300);
                userDTO.setMessage("登录失败！");
            }
        }
        userDTO.setContent(user);
        response.sendRedirect("http://localhost:8080");
        return null;
    }

    @GetMapping("checkWxStatus")
    public UserDTO checkWxStatus(){
        return this.userDTO;
    }

    @GetMapping("logout")
    public Object logout(){
        this.userDTO = null;
        return null;
    }
}
